package org.pearl.springbootsecurity.demo.config;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by TD on 2021/8/16
 */
@Component
public final class RedisUtil {

    private static RedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

    public static RedisTemplate<String, Object> getRedisTemplate() {
        return redisTemplate;
    }


    public static void setList(String cacheKey, List<?> value) {
        if (StrUtil.isNotBlank(cacheKey) && redisTemplate != null) {
            redisTemplate.delete(cacheKey);
            redisTemplate.opsForList().rightPushAll(cacheKey, value);
        }
    }

    public static <T> List<T> getList(String cacheKey) {
        List result = new ArrayList();
        if (StrUtil.isNotBlank(cacheKey) && redisTemplate != null) {
            long size = redisTemplate.opsForList().size(cacheKey);
            result = redisTemplate.opsForList().range(cacheKey, 0, size);
        }
        return result;
    }

}

